<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { AlarmRecordModel } from '#/api/equipment/models';

import { Page } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  closeAlarmRecordApi,
  getAlarmRecordPagedListApi,
} from '#/api/equipment/alarmRecord';
import { $t } from '#/locales';

import { useColumns, useSearchSchema } from './data';

/**
 * 关闭设备报警记录
 */
function onClose(row: AlarmRecordModel) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.content]),
    duration: 0,
    key: 'action_process_msg',
  });
  closeAlarmRecordApi(row.id)
    .then(() => {
      message.success({
        content: $t('ui.actionMessage.deleteSuccess', [row.content]),
        key: 'action_process_msg',
      });
      refreshGrid();
    })
    .catch(() => {
      hideLoading();
    });
}

/**
 * 表格操作按钮的回调函数
 */
function onActionClick({ code, row }: OnActionClickParams<AlarmRecordModel>) {
  switch (code) {
    case 'close': {
      onClose(row);
      break;
    }
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    collapsed: false,
    fieldMappingTime: [['rangeDate', ['startTime', 'endTime']]],
    schema: useSearchSchema(),
    // 控制表单是否显示折叠按钮
    showCollapseButton: true,
    submitOnEnter: true,
  },
  gridOptions: {
    columns: useColumns(onActionClick),
    border: true,
    stripe: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getAlarmRecordPagedListApi({
            pageNumber: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
  } as VxeTableGridOptions,
});

/**
 * 刷新表格
 */
function refreshGrid() {
  gridApi.query();
}
</script>

<template>
  <Page auto-content-height>
    <Grid />
  </Page>
</template>
